<!--
 * @Description: 告警联系人管理页面
-->
<template>
  <BaseMainSlot>
    <template v-slot:btn-box>
      <ActionBar
        :page-no="95"
        :add-active="addActive"
        :del-active="delActive"
        :edit-active="editActive"
        :cancel-active="cancelActive"
        :has-cancel="false"
        :has-save="false"
        :has-edit="false"
        :save-active="saveActive"
        @add="add"
        @cancel="cancel"
        @edit="edit"
        @del="del"
        @save="save"
      />
    </template>
    <template v-slot:search-box>
      <SearchForm
        :formConfig="formConfig"
        :formData="searchlist"
        @search="search"
      ></SearchForm>
    </template>
    <template v-slot:table-box>
      <el-table
        v-loading="loading"
        :data="tableData"
        stripe
        border
        height="calc(100% - 50px)"
        @row-click="handleEdit"
        @selection-change="handleSelectionChange"
        @sort-change="changeTableSort"
      >
        <el-table-column
          type="selection"
          width="55"
          :selectable="
            (row) => {
              return !row.indflag;
            }
          "
        />
        <el-table-column type="index" label="序号" width="100" />
        <el-table-column prop="contactsName" label="联系人名称" />
        <el-table-column prop="phone" label="手机号" />
        <el-table-column prop="weChat" label="微信" />
        <el-table-column prop="email" label="邮箱" />
        <el-table-column width="120" label="操作">
          <template v-if="scope.row.contactsNo" slot-scope="scope">
            <el-button
              v-if="judgePermission(95, 'edit')"
              class="text-btn-cursor"
              type="text"
              size="small"
              @click="edit"
            >
              <i class="el-icon-edit-outline"></i> 修改
            </el-button>
            <el-button
              v-if="judgePermission(95, 'delete')"
              class="text-btn-cursor cursor-delete"
              type="text"
              size="small"
              @click="handleDelete(scope.row.contactsNo)"
            >
              <i class="el-icon-delete"></i> 删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :page-size="pageSize"
        :current-page="currentPage"
        :total-size="totalSize"
        @changePageSize="changePageSize"
        @changeCurrentPage="changeCurrentPage"
      />
    </template>
    <template v-slot:dialog>
      <el-dialog
        :title="titleText + '告警联系人信息'"
        :visible.sync="dialogFormVisible"
        :before-close="cancel"
        width="40%"
      >
        <el-form
          ref="formData"
          label-position="left"
          :model="formData"
          :rules="rules"
          :show-message="canInput"
          class="el-form"
        >
          <el-form-item label="关联用户:" class="w100p" prop="personNo">
            <!-- <el-input v-model="formData.personNo" clearable /> -->
            <el-select
              v-model="formData.personNo"
              class="w50p"
              clearable
              filterable
              @change="personData(formData)"
            >
              <el-option
                v-for="items in StaffData"
                :key="items.person.personNo"
                :label="items.person.name"
                :value="items.person.personNo"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="联系人名称:" class="w100p" prop="contactsName">
            <el-input v-model="formData.contactsName" clearable />
          </el-form-item>
          <el-form-item label="手机号:" class="w100p" prop="phone">
            <el-input v-model="formData.phone" clearable />
          </el-form-item>
          <el-form-item label="微信:" prop="weChat" class="w100p">
            <el-input v-model="formData.weChat" clearable />
          </el-form-item>
          <el-form-item label="邮箱:" prop="email" class="w100p">
            <el-input v-model="formData.email" clearable />
          </el-form-item>
        </el-form>
        <div v-if="nowChanceStau !== ''" slot="footer" class="dialog-footer">
          <el-button class="zh-button-one" @click="cancel('formData')">
            取 消
          </el-button>
          <el-button
            class="zh-button-two"
            type="primary"
            @click="save('formData')"
          >
            保 存
          </el-button>
        </div>
      </el-dialog>
    </template>
  </BaseMainSlot>
</template>

<script>
import SearchForm from "@/components/searchForm";
import ActionBar from "@/components/ActionBar";
import Pagination from "@/components/Pagination";
import nowpagereq from "@/api/digital-production/monitoring-alarm/alarm-contact-management";
import basicInfoManage from "@/api/digital-operation/personnel-cost-mansge/staff-manage";
import BaseMainSlot from "@/components/BaseMainSlot/newBaseMainSlot";
import { mymixin } from "@/utils/mixins";
import { mapGetters } from "vuex";
export default {
  name: "AlarmContactManagement",
  components: { SearchForm, ActionBar, Pagination, BaseMainSlot },
  mixins: [mymixin],
  data() {
    return {
      formConfig: [
        {
          label: "联系人",
          prop: "contactsName",
          type: "input",
        },
        {
          label: "手机号",
          prop: "phone",
          type: "input",
        },
        {
          label: "邮箱",
          prop: "email",
          type: "input",
        },
        {
          label: "微信",
          prop: "weChat",
          type: "input",
        },
      ],
      rules: {
        contactsName: [
          {
            required: true,
            message: "请填写姓名",
            trigger: ["blur", "change"],
          },
        ],
        personNo: [
          {
            required: true,
            message: "请选择用户",
            trigger: ["blur", "change"],
          },
        ],
        phone: [
          {
            required: true,
            message: "手机号不能为空",
            trigger: "blur",
          },
          {
            pattern:
              /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
            message: "请输入正确电话号码",
          },
        ],
        email: [
          {
            message: "请输入邮箱地址",
            trigger: ["blur", "change"],
          },
          {
            pattern: /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/,
            message: "请输入正确的邮箱地址",
          },
        ],
        weChat: [
          {
            message: "请输入微信号",
            trigger: ["blur", "change"],
          },
          {
            pattern: /^[a-zA-Z][a-zA-Z\d_-]{5,19}$/,
            message: "请输入正确的微信号",
          },
        ],
      },
      // 搜索内容
      searchlist: {},
      // 选中的数据项数组 alarmContactId组成 (此数组最多为一个)
      selectIdList: [],
      StaffData: [],
      dialogTitle: "",
      dialogFormVisible: false,
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission",
    }),
  },
  watch: {},
  created() {
    // 获取表格数据
    this.getTableDataList();
    this.queryDataList();
  },
  mounted() {},
  methods: {
    getTableDataList() {
      this.loading = true;
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        sortList: this.sortList || [],
        entity: {
          organizationNo: sessionStorage.organizationNo,
          ...this.searchlist,
        },
      };
      nowpagereq.getlist(params).then((res) => {
        this.loading = false;
        if (res.code === 0) {
          this.$message.error("查询失败");
        }
        let data = res.data;
        // 赋值总条数
        this.totalSize = data.totalElements;
        // 赋值数组
        this.tableData = data.content;
        this.addblankmet();
        this.resetActionBar();
      });
    },
    async queryDataList() {
      // 员工
      const { data: Staff } = await basicInfoManage.getStaffListByCondition();
      this.StaffData = Staff;
    },
    personData(val) {
      this.StaffData.forEach((item) => {
        if (val.personNo === item.personNo) {
          this.$set(this.formData, "contactsName", item.person.name);
        }
      });
    },
    search() {
      this.currentPage = 1;
      this.getTableDataList();
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.getTableDataList();
    },
    changeCurrentPage(currentPage) {
      this.currentPage = currentPage;
      this.getTableDataList();
    },
    // // 添加
    // add() {
    //   this.openDialog("add");
    //   this.formData = {};
    // },
    // // 取消
    // cancel() {
    //   this.closeDialog();
    //   this.formData = {};
    // },
    handleEdit(row) {
      this.formData = JSON.parse(JSON.stringify(row));
      this.editActive = true;
      this.saveActive = false;
      this.cancelActive = false;
    },
    handleSelectionChange(list) {
      this.selectIdList = [];
      let notvalue = true;
      this.selectIdList = list.map((item) => {
        if (item.contactsNo) {
          return item.contactsNo;
        } else {
          notvalue = false;
          return "";
        }
      });
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
        return;
      }
      if (this.selectIdList.length > 0) this.delActive = true;
      else this.delActive = false;
    },
    delmessage() {
      nowpagereq.dellist(this.selectIdList).then((res) => {
        if (res.code === 1) {
          this.$message.success("删除成功");
          this.getTableDataList();
        } else {
          this.$message.error("删除失败");
        }
      });
    },
    handleDelete(data) {
      // nowpagereq.del(data).then(res => {
      //   if (res.code === 1) {
      //     this.$message.success("删除成功");
      //     this.getTableDataList();
      //   } else {
      //     this.$message.error("删除失败");
      //   }
      // });
      this.$confirm("此操作将永久删除该条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        customClass: "myClass",
      })
        .then(() => {
          nowpagereq.del(data).then((res) => {
            if (res.code === 1) {
              this.$message.success("删除成功");
              this.getTableDataList();
            } else {
              this.$message.error("删除失败");
            }
          });
        })
        .catch(() => {
          //
          this.$message("取消删除");
        });
    },
    save() {
      this.$refs.formData.validate((valid) => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            // 编辑接口
            // this.formData.routePointMapperList = this.tagsList;
            nowpagereq.editlist(this.formData).then((res) => {
              if (res.code === 1) {
                this.$message.success("修改成功！");
                this.closeDialog();
                this.getTableDataList();
              } else {
                this.$message.error("修改失败！");
              }
            });
          } else if (this.nowChanceStau === "add") {
            this.formData.organizationNo = sessionStorage.organizationNo;
            nowpagereq.addlist(this.formData).then((res) => {
              if (res.code === 1) {
                this.$message.success("新增成功！");
                this.closeDialog();
                this.getTableDataList();
              } else {
                this.$message.error("新增失败！");
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    openDialog(type) {
      this.dialogFormVisible = true;
      this.dialogTitle = type === "add" ? "新增" : "编辑";
      this.nowChanceStau = type;
    },
    closeDialog() {
      this.dialogFormVisible = false;
      this.dialogTitle = null;
      this.nowChanceStau = "";
    },
  },
};
</script>

<style scoped lang="scss"></style>
